<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class ActivitySeckill extends Model
{
	use \base\model\ModelTrait;
	
	//模型关联，一对多关联
	public function goodsSeckillList(){
        return $this->hasMany('GoodsSeckill','activity_id');
    }
	
	//获取器-日期范围- 依赖字段 daily_num，start_date
	public function getStartDateRangeAttr($value,$data){
		if($data['daily_num'] == 0){
    		return '每天';
		}  elseif($data['daily_num'] == 1){
			return $data['start_date'];
		} else {
			$e = date("Y-m-d",strtotime("+".($data['daily_num'] - 1)." day", strtotime($data['start_date'])));
			return $data['start_date'].' / '.$e;
		}
    }
	
	//获取器-状态
	public function getStatusAttr($value,$data){
		$d =  date("Y-m-d");
		$h =  date("H:i:s");
		$dh =  date("Y-m-d H:i:s");
		$sh = explode(" - ",$data['time_range'])[0];
		$eh = explode(" - ",$data['time_range'])[1];
		if($data['daily_num'] == 0){
			if( $d < $data['start_date'] || $h < $sh){
				return '未开始';	
			} else if( $h > $eh ){
				return '已结束';	
			} else{
				return '进行中';	
			}
		}else{
			$edh = date("Y-m-d H:i:s",strtotime("+".($data['daily_num'] - 1)." day", strtotime(($data['start_date'].' '.$eh))));
			if( $d < $data['start_date'] ){
				return '未开始';	
			} else if( $dh > $edh ) {
				return '已结束';	
			}else {
				return '进行中';	
			}
		}
    }
	
	//获取器-状态
	public function getStopDateAttr($value,$data){
		return date("Y-m-d",strtotime("+".($data['daily_num'] - 1)." day", strtotime(($data['start_date']))));
    }
	
	/**
     * 错误信息
     */
	public $errorInfo;
	
	/**
     * 验证规则
     */
	protected $rule = [];
	
	/**
     * 获取秒杀活动
     * @param type $id
     * @return type
     */
	public function getList()
    {
		$model = new self;
		$model = $model->where('is_del',0);
		$model = $model->where('is_enable',1);
		$model = $model->where(function ($query) {
			$query = $query->where(function ($query) use($id) {
				$query->where('daily_num',0);
			});
			$query = $query->whereor(function ($query) use($id) {
				$query = $query->where('daily_num','<>',0);
				$query = $query->where([['','exp',\Db::raw("date_format(now(),'%Y-%m-%d %H:%i:%s') < concat(date_add(start_date, interval daily_num-1 day),'',start_time)")]]);
			});
		});
		$model = $model->field("id,name,start_date,time_range,daily_num");
		$model = $model->field("'' as start_date_range,'' as status");
		return $model->all();
    }
	
	
	/**
     * 获取秒杀商品
     * @param type $id
     * @return type
     */
	public function getGoodsList($where=[],$pageIndex=1,$pageSize=1)
    {
    	//获取秒杀活动
		$model = new self;
		$model = $model->where('is_del',0);
		$model = $model->where('is_enable',1);
		$model = $model->where($where);
		$model = $model->where(function ($query) {
			$query = $query->where(function ($query) use($id) {
				$query->where('daily_num',0);
			});
			$query = $query->whereor(function ($query) use($id) {
				$query->where('daily_num','<>',0);
				$query->where([['','exp',\Db::raw("date_format(now(),'%Y-%m-%d %H:%i:%s') < concat(date_add(start_date, interval daily_num-1 day),'',start_time)")]]);
			});
		});
		$model = $model->field("id,name,start_date,time_range,start_time,stop_time,daily_num");
		$model = $model->field("'' as stop_date,'' as start_date_range,'' as status");
		$model = $model->order('status asc');
		$result  = $model->all();
		
		//获取商品
		foreach ($result as $key => $goods) {
			$query = $goods->goodsSeckillList();
			$query->alias('a');
			$query->leftJoin('goods b','a.goods_id = b.id');
			$query->where('a.status','1');
			$query->where('b.is_del',0);
			$query->where('a.is_del',0);
			$query->field("a.activity_id,a.shop_id,a.id as seckill_id ,a.seckill_price,a.seckill_stock,a.seckill_sales,a.seckill_num,a.seckill_info,a.goods_id");
			$query->field("b.img,b.banners,b.name as goods_name,b.info,b.price,b.postage,b.unit,b.sales,b.is_customized");
			$query->page($pageIndex,$pageSize);
			$goods->goods_seckill_list = $query->select();
			if( count($goods->goods_seckill_list) < 1){
				unset($result[$key]);
			}
		}
		
		return $result;
    }
	
	/**
     * 获取秒杀活动商品
     * @param type $id
     * @return type
     */
	public function getActivityGoodsList($id,$pageIndex=1,$pageSize=20)
    {
		$model = model('GoodsSeckill');
		$model = $model->alias('a');
		$model = $model->leftJoin('goods b','a.goods_id = b.id');
		$model = $model->where('a.status','1');
		$model = $model->where('a.is_del',0);
		$model = $model->where('a.activity_id',$id);
		$model = $model->where('b.is_del',0);
		$model = $model->field("a.activity_id,a.shop_id,a.id as seckill_id ,a.seckill_price,a.seckill_stock,a.seckill_sales,a.seckill_num,a.seckill_info,a.goods_id");
		$model = $model->field("b.img,b.banners,b.name as goods_name,b.info,b.price,b.postage,b.unit,b.sales,b.is_customized");
		$model = $model->page($pageIndex,$pageSize);
		return $model->select();
    }
	
}
